package com.leo.component;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.leo.constant.Dir;
import javafx.scene.image.Image;
import javafx.util.Duration;
import org.jetbrains.annotations.NotNull;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-06  22:04
 * @Description: TODO
 */
public class SpriteComponent extends Component {

    // 上下左右四个方向
    private final AnimationChannel upChannel, downChannel, leftChannel, rightChannel;

    private AnimatedTexture animatedTexture;

    // 初始速度
    private static final int INIT_SPEED = 150;

    private double speed = INIT_SPEED;

    private Dir dir;

    // 是否正在播放
    private boolean play = false;

    public SpriteComponent() {
        Image image = FXGL.image("行走图.png");
        upChannel = getAnimationChannel(image, 12, 15);
        downChannel = getAnimationChannel(image, 0, 3);
        leftChannel = getAnimationChannel(image, 4, 7);
        rightChannel = getAnimationChannel(image, 8, 11);
        animatedTexture = new AnimatedTexture(downChannel);
        this.dir = Dir.DOWN;
    }

    @NotNull
    private static AnimationChannel getAnimationChannel(Image image, int startFrame, int endFrame) {
        return new AnimationChannel(image, 4, 160 / 4, 216 / 4, Duration.seconds(.6), startFrame, endFrame);
    }

    @Override
    public void onAdded() {
        animatedTexture.loop();
        entity.getViewComponent().addChild(animatedTexture);
    }

    @Override
    public void onUpdate(double tpf) {
        switch (dir) {
            case UP, DOWN -> {
                entity.translateY(speed * tpf);
                entity.translateX(0);
            }
            case LEFT, RIGHT -> {
                entity.translateX(speed * tpf);
                entity.translateY(0);
            }
        }

        speed = speed * 0.8;
        if (Math.abs(speed) < 1) {
            speed = 0;
            animatedTexture.stop();
            play = false;
        }
    }

    public void moveDir(Dir dir) {
        this.dir = dir;
        switch (dir) {
            case UP -> {
                speed = -INIT_SPEED;
                if (animatedTexture.getAnimationChannel() != upChannel) {
                    animatedTexture.loopAnimationChannel(upChannel);
                    play = true;
                }
            }
            case LEFT -> {
                speed = -INIT_SPEED;
                if (animatedTexture.getAnimationChannel() != leftChannel) {
                    animatedTexture.loopAnimationChannel(leftChannel);
                    play = true;
                }
            }
            case DOWN -> {
                speed = INIT_SPEED;
                if (animatedTexture.getAnimationChannel() != downChannel) {
                    animatedTexture.loopAnimationChannel(downChannel);
                    play = true;
                }
            }
            case RIGHT -> {
                speed = INIT_SPEED;
                if (animatedTexture.getAnimationChannel() != rightChannel) {
                    animatedTexture.loopAnimationChannel(rightChannel);
                    play = true;
                }
            }
        }

        if (!play) {
            animatedTexture.loop();
            play = true;
        }
    }
}
